const { ccclass, property } = cc._decorator;

@ccclass
export default class UserInfoShow extends cc.Component {

    userinfo: cc.Node = null;

    onLoad(){

        if(cc.vv == null){
            return;
        }
        cc.vv.userInfoShow = this;
        this.userinfo = cc.find('Canvas/userinfo');
        this.userinfo.active = false;
        cc.vv.utils.addClickEvent(this.userinfo, this.node, 'UserInfoShow', 'onClicked');
    }

    onClicked(){
        this.userinfo.active = false;
    }

    /**
     * 显示用户信息
     * @param name  用户姓名
     * @param userId  用户ID
     * @param iconSprite 用户头像
     * @param sex 用户性别
     * @param ip 用户IP  
     */
    show(name, userId, iconSprite: cc.Sprite, sex: number, ip: string){

        if(userId != null && userId > 0){
            this.userinfo.active = true;
            this.userinfo.getChildByName('icon').getComponent(cc.Sprite).spriteFrame = iconSprite.spriteFrame;
            this.userinfo.getChildByName('name').getComponent(cc.Label).string = name;
            this.userinfo.getChildByName('ip').getComponent(cc.Label).string = 'IP: ' + ip.replace('::ffff:', '');
            this.userinfo.getChildByName('id').getComponent(cc.Label).string = 'ID: ' + userId;

            const sex_female = this.userinfo.getChildByName('sex_famale');
            const sex_male = this.userinfo.getChildByName('sex_male');
            sex_female.active = false;
            sex_male.active = false;

            if(sex == 1){
                sex_male.active = true;
            }
            else if(sex == 2){
                sex_female.active = true;
            }
        }
    }
}
